﻿using MOBAServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using ExitGames.Threading;
using System.Collections.Concurrent;

namespace MOBAServer.Cache
{
    public class PlayerCache
    {

        #region 数据
        private int id = 0;
        /// <summary>
        /// 玩家ID对应的玩家数据
        /// </summary>
        private ConcurrentDictionary<int, PlayerModel> idModelDic = new ConcurrentDictionary<int, PlayerModel>();
        /// <summary>
        /// 账号ID对应的玩家ID
        /// </summary>
        private ConcurrentDictionary<int, int> accPlayerDic = new ConcurrentDictionary<int, int>();
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="name"></param>
        /// <param name="accID"></param>
        public void Create(string name, int accID)
        {
            PlayerModel playerModel = new PlayerModel(id, name, accID);
            accPlayerDic.TryAdd(accID, playerModel.Id);
            idModelDic.TryAdd(playerModel.Id, playerModel);
            id++;
        }
        /// <summary>
        /// 判断是否存在角色
        /// </summary>
        /// <param name="accId"></param>
        /// <returns></returns>
        public bool Has(int accId)
        {
            return accPlayerDic.ContainsKey(accId);
        }
        #endregion

        #region 在线玩家

        private SynchronizedDictionary<MobaClient, int> clientIdDic = new SynchronizedDictionary<MobaClient, int>();

        private SynchronizedDictionary<int, MobaClient> idClientDic = new SynchronizedDictionary<int, MobaClient>();
        /// <summary>
        /// 玩家上线
        /// </summary>
        /// <param name="client"></param>
        public void Online(MobaClient client,int playerId)
        {
            clientIdDic.TryAdd(client,playerId);
            idClientDic.TryAdd(playerId,client);
        }
        public void Offline(MobaClient client)
        {
            if (!clientIdDic.ContainsKey(client))
                return;
            int id = clientIdDic[client];
            if (clientIdDic.ContainsKey(client))
                clientIdDic.Remove(client);
            if (idClientDic.ContainsKey(id))
                idClientDic.Remove(id);
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        /// <returns></returns>
        public bool HasOnline(MobaClient client)
        {
            return clientIdDic.ContainsKey(client);
        }
        public bool HasOnline(int playerId)
        {
            return idClientDic.ContainsKey(playerId);
        }
        public int GetId(int accId)
        {
            int playerId = -1;
            accPlayerDic.TryGetValue(accId,out playerId);
            return playerId;
        }
        public int GetId(MobaClient client)
        {
            int playerId = -1;
            clientIdDic.TryGetValue(client, out playerId);
            return playerId;
        }
        #endregion
        public PlayerModel GetModel(int playerId)
        {
            return idModelDic[playerId];
        }
        public PlayerModel GetModel(string name)
        {
            foreach (var item in idModelDic.Values)
            {
                if (item.Name == name)
                    return item;
            }
            return null;
        }
        public PlayerModel GetModel(MobaClient client)
        {
            if(clientIdDic.ContainsKey(client))
            {
                int id = clientIdDic[client];
                return idModelDic[id];
            }
            return null;
        }
        public void AddFriend(int playerId,int friendId)
        {
            PlayerModel model1 = idModelDic[playerId];
            model1.FriendIdList.Add(friendId);
            PlayerModel model2 = idModelDic[friendId];
            model2.FriendIdList.Add(playerId);
        }
        public MobaClient GetClient(int playerId)
        {
            return idClientDic[playerId];
        }
        /// <summary>
        /// 获取好友列表
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public List<int> GetFriends(MobaClient client)
        {
            List<int> retList = new List<int>();
            PlayerModel model = GetModel(client);
            if(model!=null)
            {
                retList.AddRange(model.FriendIdList);
            }
            return retList;
        }
    }
}
